//#include "util.hpp"
//#include "level.hpp"
//#include "message.hpp"
//#include "format.hpp"
//#include "sink.hpp"
//#include "logger.hpp"
#include "../logs/log.h"
////以时间来对文件滚动切换的日志落地模块
//时间段进行滚动
//实现思想:time(nullptr) % gap  以当前系统时间，取模时间大小，可以得到当前时间段是第几个时间段
enum class TimeGap{
  GAP_SECOND,
  GAP_MINUTE,
  GAP_HOUR,
  GAP_DAY
};


class RollByTimeSink : public mylog::LogSink{
     public:
       RollByTimeSink(const std::string &basename, TimeGap gaptype):_basename(basename)
    {
      switch(gaptype){
        case TimeGap::GAP_SECOND: _gap_size = 1;          break;
        case TimeGap::GAP_MINUTE: _gap_size = 60;         break;
        case TimeGap::GAP_HOUR: _gap_size = 3600;         break;
        case TimeGap::GAP_DAY: _gap_size = 3600 * 24;     break;
      } 
         //假如为1，取模会导致gap为0，要另作处理
         _cur_gap = _gap_size == 1 ? mylog::Util::Date::getTime() : mylog::Util::Date::getTime() % _gap_size;
         std::string filename = createFile(); //创建对应的文件名
         mylog::Util::File::CreateDirectory(mylog::Util::File::Path(filename));
         _ofs.open(filename, std::ios::binary | std::ios::app);
         assert(_ofs.is_open());
    }
       //将日志消息写到文件中
       void log(const char *data, size_t len){
         time_t cur = mylog::Util::Date::getTime();
         if((cur % _gap_size) != _cur_gap){
           _ofs.close();  //关闭源文件
           std::string filename = createFile();
           _ofs.open(filename, std::ios::binary | std::ios::app);
           assert(_ofs.is_open());
         }
         _ofs.write((const char*)data, len);
         assert(_ofs.good());
       }
     private:
       //判断当前文件是否满，然后创建新的文件
       std::string createFile(){
         //获取系统时间，以时间来构造文件
         time_t t = mylog::Util::Date::getTime();
         struct tm lt;
         localtime_r(&t, &lt);
         std::stringstream filename;
         filename << _basename;
         filename << lt.tm_year + 1900;
         filename << lt.tm_mon + 1;
         filename << lt.tm_mday;
         filename << lt.tm_hour;
         filename << lt.tm_min;
         filename << lt.tm_sec;
         filename << "-";
         filename << ".log";
         return filename.str();
       }
     private:
       std::string _basename;
       std::ofstream _ofs;
       size_t _gap_size;  //时间间隔
       size_t _cur_gap;   //当前时间段
};

int main()
{  
    
//    std::string logger_name = "sync_logger";
//    mylog::LogLevel::value limit = mylog::LogLevel::value::WARN;
//    mylog::Formatter::ptr fmt(new mylog::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
//    mylog::LogSink::ptr stdout_lsp = mylog::SinkFactory::create<mylog::StdoutSink>();
//    mylog::LogSink::ptr file_lsp = mylog::SinkFactory::create<mylog::FileSink>("./logfile/test.log");
//    mylog::LogSink::ptr roll_lsp = mylog::SinkFactory::create<mylog::RollFileSink>("./logfile/roll-",1024*1024);
//    std::vector<mylog::LogSink::ptr> sinks = {stdout_lsp, file_lsp, roll_lsp};
//    mylog::logger::ptr logger(new mylog::SyncLogger(logger_name, limit, fmt, sinks));
//  
   std::unique_ptr<mylog::LoggerBuilder> builder(new mylog::GlobalLoggerBuilder());
   builder->BuildLoggerName("async_logger");
   builder->BuildLoggerType(mylog::LoggerType::Async_logger);
   builder->BuildLooperNotSafe();
   builder->BuildFormatter("[%c][%f:%l][%p]%T%m%n");
   builder->BuildLoggerLevel(mylog::LogLevel::value::WARN);
   builder->BuildSink<mylog::FileSink>("./logfile/test1.log");
   builder->BuildSink<mylog::StdoutSink>();
   builder->build();
   mylog::logger::ptr logger = mylog::LoggerManager::GetInstance().getLogger("async_logger");

   
//   DEBUG("%s","测试日志");
//   INFO("%s","测试日志");
//   WARN("%s","测试日志");
//   ERROR("%s","测试日志");
//   FATAL("%s","测试日志");
   logger->debug("%s", "测试日志");
   logger->info("%s", "测试日志");
   logger->warn("%s", "测试日志");
   logger->error("%s", "测试日志");
   logger->fatal("%s", "测试日志");
//
//   size_t count = 0;
//   while(count <= 200000){
//     //logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
//     FATAL("测试日志-%d",count++);
//   }
//  //  ////mylog::LogMsg msg(53,mylog::LogLevel::value::INFO,"main.c","root",std::string("格式化功能测试..."));
    //mylog::LogMsg msg(53,mylog::LogLevel::value::INFO,"main.c","root",std::string("格式化功能测试..."));
    ////mylog::Formatter fmt("abc%%abcabc%g%g[%d{%H:%M:%S}]%m%n");
    //mylog::Formatter fmt;
    //std::string str = fmt.Formatter::format(msg);
    //std::cout << str << std::endl;
    //mylog::LogSink::ptr stdout_lps = mylog::SinkFactory::create<mylog::StdoutSink>();
    //mylog::LogSink::ptr file_lps = mylog::SinkFactory::create<mylog::FileSink>("./logfile/test.log");
    //mylog::LogSink::ptr roll_lps = mylog::SinkFactory::create<mylog::RollFileSink>("./logfile/roll-",1024*1024);
    //mylog::LogSink::ptr time_lps = mylog::SinkFactory::create<RollByTimeSink>("./logfile/roll-",TimeGap::GAP_SECOND);
    //stdout_lps->log(str.c_str(), str.size());
    //file_lps->log(str.c_str(), str.size());
    //size_t cursize = 0;
    //size_t count = 0;
    //while(cursize < 1024 * 1024 * 10){
    //  std::string tmp = str + std::to_string(count++);
    //  roll_lps->log(tmp.c_str(), tmp.size());
    //  cursize += tmp.size();
    //}
//  //  std::cout << mylog::LogLevel::Tostring(mylog::LogLevel::value::DEBUG) << std::endl;
//  std::cout << bitLog::Util::Date::getTime() << std::endl;
//  std::string pathname = "project/abc/cde/edf";
//  bitLog::Util::File::CreateDirectory(bitLog::Util::File::Path(pathname));
  return 0;
}
